<?php
class AccountController extends Controller {
	public function index($param) {
		$this->getSmarty ();
		
		$type = $param ['type'];
		if (empty ( $type )) {
			$type = "points_history";
		}
		
		if ($type == "points_history") {
			$this->loadPointsHistory () ;
		}
		if ($type == "returns_exchanges") {
			$this->loadReturnsExchanges () ;
		}
		
		if ($type == "invite_friends") {
			$this->smarty->assign ( 'userInviteList', $this->loadInviteFrends () );
		}
		$this->smarty->assign ( 'type', $type );
		
		$this->smarty->display ( "user_center/account.tpl" );
	}
	
	
	
	

	function loadReturnsExchanges(){
		require_once SERVICE . DS . 'CommonDataService.class.php';
		require_once 'common/lang.class.php';
		$service = new CommonDataService ( $this->getDB () );
		$results = array ();
		// 获取用户自己的
		$s_user = self::getSessonUser ();
		$sql = "SELECT * FROM user_points_approval_card where userid='" . $s_user ['userid'] . "'  ORDER BY create_time ";
		
		$results = $service->getResults ( $sql );
		foreach ($results as $k => $val) {
			$val->state = $points_approval_card_state [$val->state];
			$val->checked = $points_approval_card_checked [$val->checked];
       	}
       $this->smarty->assign ( 'exhangelist', $results);
	}
	
	function loadPointsHistory(){
		require_once SERVICE . DS . 'CommonDataService.class.php';
		require_once 'common/lang.class.php';
		$service = new CommonDataService ( $this->getDB () );
		$results = array ();
		// 获取用户自己的
		$s_user = self::getSessonUser ();
		$sql = "SELECT * FROM user_points_record where userid='" . $s_user ['userid'] . "'  ORDER BY create_time ";
		$results = $service->getResults ( $sql );
		foreach ($results as $k => $val) {
			$val->points_type = $points_type[$val->points_type];
			$val->record_type = $record_type[$val->record_type];
       	}
       	
       $user_earn_points = $service->getResult("select sum(points) points from user_points_record where userid='".$_SESSION['user']['userid']."' and record_type = 0" );
	   $user_cost_points = $service->getResult("select sum(points) points from user_points_record where userid='".$_SESSION['user']['userid']."' and record_type = 1" );
		
       $this->smarty->assign ( 'history', $results);
		$this->smarty->assign ( 'totalEarn', $user_earn_points->points);
		$this->smarty->assign ( 'totalCost', $user_cost_points->points);
		
	}
	function loadInviteFrends() {
		require_once SERVICE . DS . 'CommonDataService.class.php';
		$service = new CommonDataService ( $this->getDB () );
		$results = array ();
		// 获取用户自己的
		$s_user = self::getSessonUser ();
		$sql = "SELECT DATE_FORMAT(create_time,'%Y.%m.%d') create_time,invite_user_mail,state,rebate FROM `user_invite_record` where userid='" . $s_user ['userid'] . "'  ORDER BY create_time limit 10";
		$results = $service->getResults ( $sql );
		return $results;
	}
	
	
	
	

	/**
	 * 获取session中的user对象..返回结果是数组形式
	 * Enter description here ...
	 */
	function getSessonUser() {
		if(isset($_SESSION['user'])){
			$s_user = $_SESSION['user'];
			return self::object2array($s_user);
		}else{
			return null;
		}
	}
	/**
	 *object->array
	 *
	 **/
	function object2array(&$cgi,$type=0) {
		if(is_object($cgi)) {
			$cgi = get_object_vars($cgi);
		}
		if(!is_array($cgi)) {
			$cgi = array();
		}
		foreach($cgi as $kk=>$vv) {
			if(is_object($vv)) {
				$cgi[$kk] = get_object_vars($vv);
				self::object2array($cgi[$kk],$type);
			}
			else if(is_array($vv)) {
				self::object2array($cgi[$kk],$type);
			} else {
				$v = $vv;
				$k = $kk;
				$cgi["$k"] = $v;
			}
		}
		return $cgi;
	}
}